/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    public:
        int sumNumbers(TreeNode* root) {
            return dfs(root,0);
        }
        int dfs(TreeNode* root,int prevsum){
            int cur = prevsum*10+root->val;
            if(root->left == nullptr && root->right == nullptr) return cur;
    
            //if(root->left == nullptr && root->right != nullptr) return dfs(root->right,cur);
            //else if(root->left != nullptr && root->right == nullptr)    return dfs(root->left,cur);
            //else  return dfs(root->left,cur)+dfs(root->right,cur);
            int ret = 0;
            if(root->left) ret+= dfs(root->left,cur);
            if(root->right) ret+= dfs(root->right,cur);
            return ret;
        }
    };